package 线程;




import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class 线程实现文件复制 extends JFrame implements ActionListener{
    private JTextField tf1,tf2;
    private JProgressBar j;//设置进度条
    private JButton bt1,bt2,cp;
    File srcFile=null;
    String desPath=null;
    public 线程实现文件复制(){
        setTitle("文件复制");
        setSize(600,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);
        //创建两个JLable ，设置“选择文件”和“复制到”的位置
        JLabel label1=new JLabel("选择文件");
        JLabel label2=new JLabel("复制到");
        label1.setBounds(50,50,60,25);
        label2.setBounds(50,100,60,25);
        add(label1);add(label2);
        tf1=new JTextField();
        tf2=new JTextField();
        tf1.setBounds(110,50,300,25);
        tf2.setBounds(110,100,300,25);
        //不允许用户更改tf1,tf2中的内容只能选择
        tf1.setEditable(false);
        tf2.setEditable(false);
        add(tf1);add(tf2);
        bt1=new JButton("...");
        bt2=new JButton("...");
        //JButton bt1=new JButton(".....");
       // JButton bt2=new JButton(".....");
        bt1.setBounds(420,50,60,25);
        bt2.setBounds(420,100,60,25);
        //add(bt1);add(bt2);
        bt1.addActionListener(this);
        bt2.addActionListener(this);
        add(bt1);add(bt2);
        cp=new JButton("复制");
        cp.setBounds(270,250,60,25);
        cp.addActionListener(this);
        add(cp);
        j=new JProgressBar();
        j.setStringPainted(true);//???????
        j.setBounds(60,175,400,25);
        add(j);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
       Object o=e.getSource();

       if (o==bt1){//选择文件
           JFileChooser fc=new JFileChooser();
          // fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
           int f=fc.showOpenDialog(this);//f为0代表打开文件
           if (f==JFileChooser.APPROVE_OPTION){
              srcFile=fc.getSelectedFile();
              tf1.setText(srcFile.getPath());
           }
           //JOptionPane.showMessageDialog(this,"选择文件");
       }
       else  if (o==bt2){//选择目的文件夹
           JFileChooser fc=new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
           int f=fc.showOpenDialog(this);//f为0代表打开文件
           if (f==JFileChooser.APPROVE_OPTION){
               desPath=fc.getSelectedFile().getPath();
               tf2.setText(desPath);
           }

       }
       else if (o==cp){//开始复制
             if (srcFile==null||desPath==null){
                 JOptionPane.showMessageDialog(this,"选啊");
                 return;
             }
         new Thread(new Runnable() {
             @Override
             public void run() {
                 File desFile=new File(desPath+"\\"+srcFile.getName());
                 try {
                     FileInputStream fis=new FileInputStream(srcFile);
                     FileOutputStream fos=new FileOutputStream(desFile);
                     byte[] tmp=new byte[1024];
                     int c=fis.read(tmp);
                     long fsize=srcFile.length();
                     int percent=0;
                     while (c!=-1){
                         fos.write(tmp,0,c);
                         percent=(int) ((1.0-1.0*fis.available()/fsize)*100);
                         j.setValue(percent);
                         c=fis.read(tmp);
                     }
                     fos.close();
                     fis.close();
                 } catch (IOException e1) {
                     e1.printStackTrace();
                 }
             }
         }).start();

       }
    }
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new 线程实现文件复制();
            }
        });
    }

}
